package com.example.springboot.controller.business;

import com.alibaba.fastjson.JSONObject;
import com.example.springboot.aop.Release;
import com.example.springboot.entity.Blog;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.blog.service.BlogService;
import com.example.springboot.mapper.blog.service.serviceImp.TokenService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

/**
 * @param
 * @Description: 类描述
 * @Author: allan5945@qq.com
 * @Date: 2018/10/24
 * @return:
 * @throws:
 */
@RequestMapping(value = "/blog")
@RestController
public class BlogController {

    @Autowired
    private BlogService BlogServiceImp;
    @Autowired
    private TokenService tokenService;

    /**
     * @api {POST} /blog/getList  获取博客列表
     * @apiDescription 博客列表
     * @apiGroup Blog
     * @apiParam {String} pageNum 当前页数 ** 默认1
     * @apiParam {String} pageSize 每页的条数 ** 默认20
     * @apiSuccessExample 查询成功
     * {
     *  statusCode: 2000,
     *  mes: '查询成功',
     *  data:{
     *   pageNum:1,  // 当前页数
     *   pageSize:5,  // 每页的条数
     *   num:10,  // 总条数
     *   listNum:[
     *    {         // 具体博客内容
     *      id:1，//博客唯一id
     *      belongerId:10, //博客归属人id
     *      blogText:"", //博客内容
     *      blogTag:"", //博客归属类别
     *      blogTitle:"", //博客title
     *      zanNum:10, //博客点赞次数
     *      seeNum:10, //博客查看次数
     *      time:"2016-06-12"  // 创建时间
     *     }
     *   ],
     *  }
     * @apiError (40xx) {Number} 4005 传入页数大于总页数s
     * @apiErrorExample 传入参数异常
     * {
     *  statusCode: 4005,
     *  data:null,
     *  mes: '传入参数异常', // 传入页数大于总页数
     * }
     * @apiVersion 1.0.1
     */
    @Release
    @RequestMapping(value = "getList")
    public Map getBlogList(Integer pageNum, Integer pageSize, String conText){
        Integer statusCode;
        String mes;
        Object data = null;
        ArrayList<String> con = new ArrayList<>();
        Map<String, Object> blogList = new HashMap<>();
        if(pageNum == null){
            pageNum = 1;
        };
        if(pageNum == null){
            pageSize = 20;
        };
        if(conText != null){
            con = new ArrayList<String>(Arrays.asList(conText.split(",")));
        }
        Integer num = BlogServiceImp.blogeCount(con);
        if((pageNum -1) * pageSize > num){
            statusCode = 4005;
            mes = "传入参数异常";
        }else{
            List list = BlogServiceImp.selectHotBlog(pageNum,pageSize,con);
            Map<String, Object> dat = new HashMap<>();
            dat.put("num",num);
            dat.put("listNum",list);
            dat.put("pageNum",pageNum);
            dat.put("pageSize",pageSize);
            data = dat;
            statusCode = 2000;
            mes = "查询成功";
        };
        blogList.put("statusCode",statusCode);
        blogList.put("mes",mes);
        blogList.put("data",data);
        return blogList;
    }

    @Release
    @RequestMapping(value = "getBlog")
    public JSONObject getBlog(Integer id){
        JSONObject data = new JSONObject();
        Blog b = BlogServiceImp.selectBlog(id);
        data.put("data",b);
        data.put("statusCode",2000);
        return data;
    }


    @RequestMapping(value = "addBlog")
    public Map addBlog(String blogText,String blogTitle,String blogTag){
        HashMap<String, Object> mps = new HashMap<>();
        Blog blog = new Blog();
        blog.setBelongerId(tokenService.getUserInfo().getId());
        blog.setBlogText(blogText);
        blog.setBlogTag(blogTag);
        blog.setBlogTitle(blogTitle);
        BlogServiceImp.addBlog(blog);
        mps.put("data",null);
        mps.put("statusCode",2000);
        return mps;
    }

    @Release
    @RequestMapping(value = "getHotTag")
    public Map getHotTag(){
        HashMap<String, Object> mps = new HashMap<>();
        List<String> ls = BlogServiceImp.getHotTag(1,10);
        mps.put("data",ls);
        mps.put("statusCode",2000);
        return mps;
    }
    @RequestMapping(value = "getTag")
    public Map getTag(){
        HashMap<String, Object> mps = new HashMap<>();
        tokenService.getUserInfo();
        List<String> ls = BlogServiceImp.getTag(tokenService.getUserInfo().getId());
        mps.put("data",ls);
        mps.put("statusCode",2000);
        return mps;
    }

    @RequestMapping(value = "getUserBlog")
    public Map selectUserBlog(){
        HashMap<String, Object> mps = new HashMap<>();
        List<Blog> blogs = BlogServiceImp.selectUserBlog(tokenService.getUserInfo().getId());
        mps.put("data",blogs);
        mps.put("statusCode",2000);
        return mps;
    }

    @RequestMapping(value = "delectBlog")
    public Map delectBlog(Integer id){
        HashMap<String, Object> mps = new HashMap<>();
        Boolean d = BlogServiceImp.delectBlog(id,tokenService.getUserInfo().getId());
        mps.put("data",d);
        mps.put("statusCode",2000);
        return mps;
    }
}
